package com.yangchoi.jetpackcompose.ui.widget.component

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import coil.annotation.ExperimentalCoilApi
import coil.compose.rememberImagePainter
import coil.transform.RoundedCornersTransformation
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.yangchoi.jetpackcompose.R
import com.yangchoi.jetpackcompose.theme.white_16sp
import kotlinx.coroutines.delay

const val TestImgUrl = "https://lmg.jj20.com/up/allimg/1114/062621110J7/210626110J7-10-1200.jpg"

/**
 * @param dataList 数据列表
 * @param bannerHeight banner高度
 * @param auto 是否自动播放
 * @param autoTime 轮播时间
 */
@OptIn(ExperimentalPagerApi::class, ExperimentalCoilApi::class)
@Composable
fun <T> BannerWidget(
    dataList: ArrayList<T>,
    bannerHeight: Dp = 150.dp,
    auto:Boolean = true,
    autoTime:Long = 3000L,
    bannerContent: @Composable (index:Int, item:T)-> Unit
) {

    val startIndex = 0
    val pagerState = rememberPagerState(initialPage = startIndex)

    var currentTime by remember { mutableStateOf(10L) }

    if (auto) {
        LaunchedEffect(key1 = currentTime) {
            delay(autoTime)
            if (pagerState.currentPage == dataList.size - 1) {
                pagerState.animateScrollToPage(0)
            } else {
                pagerState.animateScrollToPage(pagerState.currentPage + 1)
            }
            currentTime = System.currentTimeMillis()
        }
    }

    ConstraintLayout(modifier = Modifier
        .fillMaxWidth()
        .height(bannerHeight)) {
        val (indiator) = createRefs()
        HorizontalPager(
            count = dataList.size,
            state = pagerState,
            modifier = Modifier.fillMaxSize()
        ) { index ->
            bannerContent(index,dataList[index])
        }
        LazyRow(modifier = Modifier.constrainAs(indiator){
            end.linkTo(parent.end,10.dp)
            bottom.linkTo(parent.bottom,20.dp)
        }){
            itemsIndexed(dataList){index, item ->
                if(index == pagerState.currentPage){
                    Box(
                        modifier = Modifier
                            .padding(end = 10.dp)
                            .width(20.dp)
                            .height(4.dp)
                            .background(Color.White, RoundedCornerShape(8.dp))
                    )
                }else{
                    Box(
                        modifier = Modifier
                            .padding(end = 10.dp)
                            .size(4.dp)
                            .background(Color.White, CircleShape)
                    )
                }
            }
        }
    }
}

@Composable
fun BannerItem(item:String,desc:String,index:Int){
    ConstraintLayout(modifier = Modifier.fillMaxSize()) {
        val (bottomContent) = createRefs()
        LoadNetWorkImage(
            imgUrl = item, modifier = Modifier
                .fillMaxSize()
                .clip(RoundedCornerShape(8.dp))
        )

        Row(
            modifier = Modifier
                .padding(horizontal = 10.dp)
                .fillMaxWidth()
                .constrainAs(bottomContent) {
                    bottom.linkTo(parent.bottom, 20.dp)
                },
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(
                text = desc,
                style = white_16sp,
                modifier = Modifier
                    .padding(end = 10.dp)
                    .widthIn(max = 300.dp)
            )
        }
    }
}